#!/bin/bash

# sudo nano /etc/cron.d/limitcpu
# 添加一行
#
# * * * * * root /pathtofile/limitcpu.sh >> /var/log/limitcpu.log 2>&1
#
# sudo /etc/init.d/cron reload

do_decrease()
{
    PIDS=`top -bn 1 | grep "^ *[1-9]" | awk '{ if($9 > 80 || $10 > 40 && id -u $2 > 500) print $1}'`
    
    for PID in $PIDS
    do
        renice +15 $PID
    done
}

do_increase()
{
    PIDS=`top -bn 1 | grep -E "Xorg|lightdm" | awk '{print $1}'`

    for PID in $PIDS
    do
        renice -10 $PID
    done
}

#################################################

date +%F~%T

for((i=0;i<6;i++))
do
    echo $i
    do_decrease
    sleep 10
done

do_increase
